/**
 * Copyright (c) 2011 Technical Centre Radio Systems Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.tcrs.tabula.config;

import ua.tcrs.tabula.util.Joiner;
import ua.tcrs.tabula.util.Iterables;
import ua.tcrs.tabula.util.Predicates;

public abstract class Section {
	
	public abstract Iterable<String> keys();
	public Iterable<String> keys( final String prefix ) {
		return Iterables.filter( keys(), Predicates.startsWith( prefix ) );
	}
	
	public boolean contains( String name ) {
		return getInternal( name )!=null;
	}
	public String getString( String name, String def ) {
		String value = getInternal( name );
		return (value==null?def:value);
	}
	public Boolean getBoolean(String name, boolean def) {
		String value = getInternal(name);
		return (value == null ? def : value.equalsIgnoreCase("true"));
	}
	public int getInt( String name, int def ) {
		String value = getInternal( name );
		return (value==null?def:Integer.parseInt(value));
	}
	public int getIntSafe( String name, int def ) {
		try {
			return getInt( name, def );
		} catch ( NumberFormatException e ) {
			return def;
		}
	}
	public long getLong( String name, long def ) {
		String value = getInternal( name );
		return (value==null?def:Long.parseLong(value));
	}
	public long getLongSafe( String name, long def ) {
		try {
			return getLong( name, def );
		} catch ( NumberFormatException e ) {
			return def;
		}
	}
	public String[] getStringArray( String name, String sep, String... defs ) {
		String value = getInternal( name );
		// String.split uses regexp instead of separator!
		return (value==null?defs:value.split(sep));
	}
	public int[] getIntArray( String name, String sep, int... defs ) {
		String value = getInternal( name );
		if ( value == null ) {
			return defs;
		} else {
			String[] splitted = value.split(sep);
			int[] result = new int[splitted.length];
			for ( int idx=0; idx<splitted.length; idx++ ) {
				result[idx] = Integer.parseInt(splitted[idx]);
			}
			return result;
		}
	}
	public long[] getLongArray( String name, String sep, long... defs ) {
		String value = getInternal( name );
		if ( value == null ) {
			return defs;
		} else {
			String[] splitted = value.split(sep);
			long[] result = new long[splitted.length];
			for ( int idx=0; idx<splitted.length; idx++ ) {
				result[idx] = Long.parseLong(splitted[idx]);
			}
			return result;
		}
	}
	
	public Section set( String name, Object value ) {
		setInternal( name, (value==null?null:value.toString()) );
		return this;
	}
	public Section setArray( String name, String sep, Object... values ) {
		setInternal( name, new Joiner( sep ).join( values ) );
		return this;
	}
	
	protected abstract String getInternal( String name );
	protected abstract void setInternal( String name, String value );
}
